package com.yong.sbd.biz.file.controller;

import com.yong.sbd.biz.file.service.FileService;
import com.yong.sbd.common.beans.response.R;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传下载
 *
 * @author Kang Yong
 * @date 2025/10/24
 * @since 1.0.0
 */
@RestController
@RequestMapping("/file")
@AllArgsConstructor
public class FileController {

    private final FileService fileService;

    /**
     * 文件上传
     *
     * @param file {@link MultipartFile}
     * @return {@link R< String>} 返回文件名
     * @author KangYong
     * @date 2025/10/24
     */
    @PostMapping("/upload")
    public R<String> upload(@RequestParam("file") MultipartFile file) {
        return R.success(fileService.upload(file));
    }

    /**
     * 文件下载
     *
     * @param filePath {@link String}
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {@link R< Void>}
     * @author KangYong
     * @date 2025/10/24
     */
    @GetMapping("/download")
    public R<Void> download(@RequestParam("filePath") String filePath,
                            @RequestParam("dataFolder") String dataFolder,
                            HttpServletRequest request, HttpServletResponse response) {
        fileService.download(filePath, dataFolder, request, response);
        return R.success();
    }
}
